﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.Configuration;

public partial class PresentDemand : System.Web.UI.Page
{
    private Operation op = new Operation();
    string[] procs;
    protected void Page_Load(object sender, EventArgs e)
    {
        
            BindData();
            Calendar1.Visible = false;
       
        
             
       
    }

    private void BindData()
    {
        ddlProcess.Items.Clear();
           string process;
            process = ConfigurationManager.AppSettings["process"];
            procs = process.Split(';');
            for (int i = 0; i < procs.Length; i++)
            {
                ddlProcess.Items.Add(procs[i]);
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                CheckBox cb = new CheckBox();
                TextBox tb = new TextBox();
                Label lb = new Label();
                lb.Text = "版本";
                tb.Width = 41;
                cb.Text = procs[i];
                cb.ID = "cb" + procs[i];
                cell.Controls.Add(cb);
                row.Cells.Add(cell);
                cell = new TableCell();
                cell.Controls.Add(lb);
                row.Cells.Add(cell);
                cell = new TableCell();
                tb.ID = "tb" + procs[i];
                cell.Controls.Add(tb);
                row.Cells.Add(cell);
                Table1.CellSpacing = 20;
                Table1.Rows.Add(row);
            }
    }

   
   
    protected void btnAppend_Click(object sender, EventArgs e)
    {
        //验证产品名称和需求时间是否为空
        if (txtProductName.Text.Length == 0 || tbDemandDate.Text.Length == 0)
        {
            Response.Write("<script language='javascript'>alert('产品名称和需求时间不能为空，请检查！');</script>");
            return;
        }
        //判断是否上传成功
        bool flag =false;
        #region 单工序
        if (rbSingleProcess.Checked)
        {           
            
            if (txtVersion.Text.Length == 0)
            {
                Response.Write("<script language='javascript'>alert('版本号不能为空，请检查！');</script>");
                return;
            }
            Demand demand = new Demand();
            demand.Platform = txtPlatform.Text;
            demand.Productname = txtProductName.Text;
            demand.Process = ddlProcess.SelectedItem.ToString();
            demand.Demandtime = tbDemandDate.Text;
            demand.Version = txtVersion.Text;
            demand.Remark = txtRemark.Text;
            flag = op.CommitDemand(demand);
        }
        #endregion

        #region 多工序
        if (rbMultiProcess.Checked)
        {          
            for (int i = 0; i < procs.Length; i++)
            {
                if (((CheckBox)FindControl("cb" + procs[i])).Checked)
                {
                    if (((TextBox)FindControl("tb" + procs[i])).Text.Length == 0)
                    {
                        Response.Write("<script language='javascript'>alert('版本号不能为空，请检查！');</script>");
                        return;
                    }
                }
            }
            for (int i = 0; i < procs.Length; i++)
            {
                if (((CheckBox)FindControl("cb" + procs[i])).Checked)
                {
                    if (((TextBox)FindControl("tb" + procs[i])).Text.Length == 0)
                    {
                        Response.Write("<script language='javascript'>alert('版本号不能为空，请检查！');</script>");
                        return;
                    }
                    Demand demand = new Demand();
                    demand.Platform = txtPlatform.Text;
                    demand.Productname = txtProductName.Text;
                    demand.Process = ((CheckBox)FindControl("cb" + procs[i])).Text;
                    demand.Demandtime = tbDemandDate.Text;
                    demand.Version = ((TextBox)FindControl("tb" + procs[i])).Text;
                    demand.Remark = txtRemark.Text;
                    flag = op.CommitDemand(demand);
                }
            }
        
        }
        #endregion

        if (flag)
        {
            Response.Write("<script language='javascript'>alert('添加需求成功！');</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('需求添加失败！');</script>");
        }
    }

    

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~\BrowserDatas.aspx");
    }

    protected void btnChooseDate_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        tbDemandDate.Text = Calendar1.SelectedDate.ToString();
        Calendar1.Visible = false;
    }
    protected void rbSingleProcess_CheckedChanged(object sender, EventArgs e)
    {
        if (((RadioButton)sender).Checked)
        {
            Table1.Enabled = false;
            ddlProcess.Enabled = true;
            txtVersion.Enabled = true;
           
        }
    }
    protected void rbMultiProcess_CheckedChanged(object sender, EventArgs e)
    {
        if (((RadioButton)sender).Checked)
        {
            ddlProcess.Enabled = false;
            txtVersion.Enabled = false;
            Table1.Enabled = true;
        }
    }
}